package com.zqll.gdx.starter.assets;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.widget.*;
import com.zqll.gdx.starter.assets.utils.SkinFonts;

/**
 * VisUI 演示（用于测试第三方样式下的动态字体替换）
 * 使用 vis-ui x2 皮肤，构建与 GameMenuDemo 不同的界面结构。
 */
public class VisUiDemo extends ApplicationAdapter {
    public static void main(String[] args) {
        Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
        config.setWindowedMode(900, 640);
        new Lwjgl3Application(new VisUiDemo(), config);
    }

    private Stage stage;
    private Skin skin;

    private VisTable root;
    private MenuBar menuBar;
    private VisSplitPane splitPane;
    private VisTree leftTree;
    private VisTable rightPanel;

    private VisWindow controlWindow;
    private VisProgressBar progressBar;

    @Override
    public void create() {
        StarterAssets.init();
        StarterAssets.loadDefaults();

        // 加载 vis-ui x2 皮肤
        skin = new Skin(Gdx.files.internal("gdx-skins/vis-ui/x2/uiskin.json"));

        // 先将皮肤设置到 VisUI（以默认字体构建界面）
        VisUI.load(skin);

        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);

        buildUI();

        // 动态替换：按 JSON 名称将 default-font / small-font 替换为中文字体
        SkinFonts.replaceFont(skin, "default-font", StarterAssets.font(18), true, true);
        SkinFonts.replaceFont(skin, "small-font", StarterAssets.font(14), true, true);
    }

    private void buildUI() {
        root = new VisTable(true);
        root.setFillParent(true);
        stage.addActor(root);

        // 顶部菜单栏
        menuBar = new MenuBar();
        Menu fileMenu = new Menu("文件");
        fileMenu.addItem(new MenuItem("新建", new ChangeListener() {
            @Override public void changed(ChangeEvent event, Actor actor) { Gdx.app.log("VisUiDemo", "新建"); }
        }));
        fileMenu.addItem(new MenuItem("保存", new ChangeListener() {
            @Override public void changed(ChangeEvent event, Actor actor) { Gdx.app.log("VisUiDemo", "保存"); }
        }));
        fileMenu.addItem(new MenuItem("退出", new ChangeListener() {
            @Override public void changed(ChangeEvent event, Actor actor) { Gdx.app.exit(); }
        }));

        Menu viewMenu = new Menu("视图");
        viewMenu.addItem(new MenuItem("切换主题", new ChangeListener() {
            @Override public void changed(ChangeEvent event, Actor actor) { Gdx.app.log("VisUiDemo", "切换主题"); }
        }));
        menuBar.addMenu(fileMenu);
        menuBar.addMenu(viewMenu);
        root.add(menuBar.getTable()).growX().row();

        // 左侧树 + 右侧面板
        leftTree = new VisTree();
        VisTree.Node missions = new VisTree.Node(new VisLabel("任务")) {};
        missions.add(new VisTree.Node(new VisLabel("主线任务")) {});
        missions.add(new VisTree.Node(new VisLabel("支线任务")) {});
        VisTree.Node settings = new VisTree.Node(new VisLabel("设置")) {};
        settings.add(new VisTree.Node(new VisLabel("图形")) {});
        settings.add(new VisTree.Node(new VisLabel("声音")) {});
        leftTree.add(missions);
        leftTree.add(settings);
        leftTree.expandAll();
        VisScrollPane leftScroll = new VisScrollPane(leftTree);

        rightPanel = new VisTable(true);
        rightPanel.top().pad(8);
        rightPanel.add(new VisLabel("控制面板")).left().row();

        // 文本与输入（测试 messageFont）
        rightPanel.add(new VisLabel("玩家名称")).left();
        VisTextField nameField = new VisTextField();
        nameField.setMessageText("请输入名称...");
        rightPanel.add(nameField).growX().row();

        // 选择框与按钮
        rightPanel.add(new VisLabel("难度等级")).left();
        VisSelectBox<String> difficulty = new VisSelectBox<>();
        difficulty.setItems("简单", "普通", "困难");
        rightPanel.add(difficulty).left().row();

        VisCheckBox cbMusic = new VisCheckBox("背景音乐");
        cbMusic.setChecked(true);
        rightPanel.add(cbMusic).left();
        VisTextButton startBtn = new VisTextButton("开始游戏");
        rightPanel.add(startBtn).right().row();

        // 列表和进度
        VisList<String> list = new VisList<>();
        list.setItems("关卡 1", "关卡 2", "关卡 3", "关卡 4");
        rightPanel.add(new VisScrollPane(list)).growX().height(120).colspan(2).row();

        progressBar = new VisProgressBar(0f, 100f, 1f, false);
        progressBar.setValue(25f);
        rightPanel.add(new VisLabel("加载进度")).left();
        rightPanel.add(progressBar).growX().row();

        // 可拖拽窗口
        controlWindow = new VisWindow("系统信息");
        controlWindow.add(new VisLabel("分辨率: ")).left();
        controlWindow.add(new VisLabel(Gdx.graphics.getWidth() + "x" + Gdx.graphics.getHeight())).left().row();
        controlWindow.add(new Separator()).growX().row();
        controlWindow.add(new VisLabel("提示: 可拖拽此窗口")).left().row();
        controlWindow.pack();
        controlWindow.setPosition(600, 380);
        stage.addActor(controlWindow);

        // 分割面板
        splitPane = new VisSplitPane(leftScroll, new VisScrollPane(rightPanel), false);
        splitPane.setSplitAmount(0.3f);
        root.add(splitPane).grow().row();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0.08f, 0.1f, 0.12f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        if (progressBar != null && progressBar.getValue() < 100f) {
            progressBar.setValue(progressBar.getValue() + 0.15f);
        }
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
        VisUI.dispose();
        StarterAssets.dispose();
    }
}